home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  2.9 KB  |  112 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////////////
  6. //Only when compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. #define false FALSE
  10. ////////////////////////////////////////////////////////////
  11.  
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. //Information about the prototype list to enable nodes to register themselves
  17. extern CGPNode **pPrototypeList;
  18. extern unsigned long ulNumberOfPrototypes;
  19.  
  20. CGPNode::CGPNode()
  21. {
  22.     //Initialise the node's name
  23.     psName=new char[25];
  24.     sprintf(psName,"BaseClassNode");
  25.  
  26.     //Default properties
  27.     ulNumberOfChildren=0;
  28.     pChildren=NULL;
  29.     nType=3;
  30. }
  31.  
  32. CGPNode::~CGPNode()
  33. {
  34.     delete []psName;
  35. }
  36.  
  37. int CGPNode::AddToPrototypeList()
  38. {
  39.     //This function is called wheever a node is constrcuted. First, a check is made to see if a node
  40.     //of the same type is already present in the prototype list. If so, the new node is not added. If this
  41.     //type is not yet represented in the prototype list, this node must be a prototype, so we add it
  42.     unsigned long i;
  43.  
  44.     //Check to see if a node of this type is already listed
  45.     int nIsIn=0;
  46.     for(i=0;i<ulNumberOfPrototypes;i++)
  47.     {
  48.         if(pPrototypeList[i]->nType==nType)
  49.         {
  50.             nIsIn=1;
  51.         }
  52.     }
  53.  
  54.     //If not,
  55.     if(!nIsIn)
  56.     {
  57.         //Create a new prototype list that does contain it
  58.         CGPNode **pOldPrototypeList=pPrototypeList;
  59.         ulNumberOfPrototypes++;
  60.         pPrototypeList=new CGPNode*[ulNumberOfPrototypes];
  61.         for(i=0;i<ulNumberOfPrototypes-1;i++)
  62.         {
  63.             pPrototypeList[i]=pOldPrototypeList[i];
  64.         }
  65.         pPrototypeList[i]=this;
  66.  
  67.         //Remeber to get rid of the old one!
  68.         if(pOldPrototypeList!=NULL)
  69.         {
  70.             delete []pOldPrototypeList;
  71.         }
  72.     }
  73.     return nIsIn;
  74. }
  75.  
  76. CGPNode *CGPNode::pGetCopy(CGP*)
  77. {
  78.     //In real nodes, this function creates and returns an exact copy of of the class and its contents
  79.     assert(false);
  80.     return NULL;
  81. }
  82.  
  83. double CGPNode::dEvaluate(void)
  84. {
  85.     //Forces the node to evaluate itself (see the header file for more detail, and one of the real nodes for a 
  86.     //specific example)
  87.     assert(false);
  88.     return NULL;
  89. }
  90.  
  91. char* CGPNode::psGetString(char*)
  92. {
  93.     //This function appends a string representing the function of the subprogram attached to this node to the string
  94.     //passed to this function. See one of the real nodes for a concrete example
  95.     assert(false);
  96.     return NULL;
  97. }
  98.  
  99. unsigned long CGPNode::ulGetNumberOfNodesInSubtree(unsigned long ulNodesFoundSoFar)
  100. {
  101.     //This function allows the calling function to count the number of nodes in the subprogram attached to this
  102.     //node. Used mainly when mutation and crossover points.
  103.     return ulNodesFoundSoFar;
  104. }
  105.  
  106. void CGPNode::GetnthNode(unsigned long&,unsigned long,CGPNode**&)
  107. {
  108.     //This function gives a pointer to the nth node in the subtree attached to this node. Again, used mainly 
  109.     //when mutation and crossover points.
  110. }
  111.  
  112.